home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / programer2 / icon / Source / Icont / C / Trans < prev    next >
Encoding:
Text File  |  1991-05-25  |  4.4 KB  |  191 lines

  1. /*
  2.  * trans.c - main control of the translation process.
  3.  */
  4.  
  5. #include "../h/config.h"
  6. #include "general.h"
  7. #include "tproto.h"
  8. #include "../h/version.h"
  9. #include "globals.h"
  10. #include "trans.h"
  11. #include "tsym.h"
  12. #include "tree.h"
  13. #include "token.h"
  14.  
  15. /*
  16.  * Prototypes.
  17.  */
  18.  
  19. hidden    FILE    *preprocess    Params((char *filename));
  20. hidden    novalue    trans1        Params((char *filename));
  21.  
  22. char *comfile = NULL;
  23.  
  24. int tfatals;            /* total number of fatal errors */
  25. int nocode;            /* non-zero to suppress code generation */
  26. int in_line;            /* current input line number */
  27. int incol;            /* current input column number */
  28. int peekc;            /* one-character look ahead */
  29.  
  30. FILE *srcfile;            /* current input file */
  31. FILE *codefile;            /* current ucode output file */
  32. FILE *globfile;            /* current global table output file */
  33.  
  34. /*
  35.  * translate a number of files, returning an error count
  36.  */
  37. int trans(ifiles)
  38. char **ifiles;
  39.    {
  40.    tmalloc();            /* allocate memory for translation */
  41.  
  42. #ifdef MultipleRuns
  43.    yylexinit();            /* initialize lexical analyser */
  44.    tcodeinit();            /* initialize code generator */
  45. #endif                    /* Multiple Runs */
  46.  
  47.    while (*ifiles)
  48.       trans1(*ifiles++);    /* translate each file in turn */
  49.    tmfree();            /* free memory used for translation */
  50.  
  51.    /*
  52.     * Report information about errors and warnings and be correct about it.
  53.     */
  54.    if (tfatals == 1)
  55.       fprintf(stderr, "1 error\n");
  56.    else if (tfatals > 1)
  57.       fprintf(stderr, "%d errors\n", tfatals);
  58.    else if (!silent)
  59.       fprintf(stderr, "No errors\n");
  60.  
  61.    return tfatals;
  62.    }
  63.  
  64. /*
  65.  * translate one file.
  66.  */
  67. static novalue trans1(filename)
  68. char *filename;
  69. {
  70.    char oname[MaxFileName];    /* buffer for constructing file names */
  71.  
  72.    comfile = filename;
  73.    tfatals = 0;    /* reset error counts */
  74.    nocode = 0;            /* allow code generation/*
  75.    in_line = 1;            /* start with line 1, column 0 */
  76.    incol = 0;
  77.    peekc = 0;            /* clear character lookahead */
  78.  
  79.    if (m4pre) {
  80.       srcfile = preprocess(filename);
  81.       if (strcmp(filename,"-") == 0)
  82.          filename = "stdin";
  83.    }
  84.    else if (strcmp(filename,"-") == 0) {
  85.       srcfile = stdin;
  86.       filename = "stdin";
  87.       }
  88.    else
  89.       srcfile = fopen(filename,ReadText);
  90.    if (srcfile == NULL)
  91.       quitf("cannot open %s",filename);
  92.    if (!silent)
  93.       fprintf(stderr, "%s:\n",filename);
  94.  
  95. #ifndef VarTran
  96.    /*
  97.     * Form names for the .u1 and .u2 files and open them.
  98.     *  Write the ucode version number to the .u2 file.
  99.     */
  100.  
  101.    makename(oname, TargetDir, filename, U1Suffix);
  102.  
  103. #if MVS || VM
  104. /*
  105.  * Even though the ucode data is all reasonable text characters, use
  106.  *  of text I/O may cause problems if a line is larger than LRECL.
  107.  *  This is likely to be true with any compiler, though the precise
  108.  *  disaster which results may vary.
  109.  */
  110.    codefile = fopen(oname, WriteBinary);   /* avoid line splits */
  111. #else                    /* MVS || VM */
  112.    codefile = fopen(oname, WriteText);
  113. #endif                    /* MVS || VM */
  114.  
  115.    if (codefile == NULL)
  116.       quitf("cannot create %s", oname);
  117.  
  118.    makename(oname, TargetDir, filename, U2Suffix);
  119.  
  120. #if MVS || VM
  121.    globfile = fopen(oname, WriteBinary);
  122. #else                    /* MVS || VM */
  123.    globfile = fopen(oname, WriteText);
  124. #endif                    /* MVS || VM */
  125.  
  126.    if (globfile == NULL)
  127.       quitf("cannot create %s", oname);
  128.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  129. #endif                    /* VarTran */
  130.  
  131.    tok_loc.n_file = filename;
  132.    in_line = 1;
  133.  
  134.    tminit();                /* Initialize data structures */
  135.    yyparse();                /* Parse the input */
  136.  
  137.    /*
  138.     * Close the output files and the input file.
  139.     */
  140.  
  141. #ifndef VarTran
  142.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  143.       quit("cannot close ucode file");
  144. #endif                    /* VarTran */
  145.  
  146.    if (!m4pre) 
  147.       fclose(srcfile);
  148.    /* "else" is below in conditional */
  149.  
  150. #if ARM || UNIX
  151.    else if (pclose(srcfile) != 0)
  152.       quit("m4 terminated abnormally");
  153. #endif                    /* ARM || UNIX */
  154.    }
  155.  
  156. /*
  157.  * writecheck - check the return code from a stdio output operation
  158.  */
  159. novalue writecheck(rc)
  160.    {
  161.    if (rc < 0)
  162.       quit("cannot write to ucode file");
  163.    }
  164.  
  165. /*
  166.  * open a pipe to the preprocessor.
  167.  */
  168. static FILE *preprocess(filename)
  169. char *filename;
  170. {
  171.  
  172. #if MACINTOSH
  173. #if MPW
  174. /* #pragma unused(filename) */
  175. #endif                    /* MPW */
  176. #endif                    /* MACINTOSH */
  177.  
  178. #if ARM || UNIX
  179.       {
  180.       FILE *f, *popen();
  181.       char *s = alloc((unsigned int)(4+strlen(filename)));
  182.       sprintf(s,"m4 %s",filename);
  183.       f = popen(s,ReadText);
  184.       free(s);
  185.       return f;
  186.       }
  187. #else                    /* ARM || UNIX */
  188.    return NULL;
  189. #endif                    /* ARM || UNIX */
  190. }
  191.